package com.web.manager;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.web.domain.ApiResponse;
import com.web.entity.Bank;
import com.web.entity.Member;
import com.web.utils.HttpUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author FDjavaone
 * @date 2017/8/23
 */
public class BankManager {

    public static void main(String[] args) {
        Bank bank = new Bank();
        bank.setCardNo("6228480322126337318");
        getBankCardInfo(bank);
    }

    public static ApiResponse getBankCardInfo(Bank bank) {
        ApiResponse ponse = new ApiResponse();
        String host = "http://jisuyhkgsd.market.alicloudapi.com";
        String path = "/bankcard/query";
        String method = "GET";
        String appcode = "2367dde9fcb5418c86b13a0bd8a3e7f6";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("bankcard", bank.getCardNo());

        try {
            HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
            String resultBody = EntityUtils.toString(response.getEntity());

            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode rootNode;

            rootNode = objectMapper.readValue(resultBody, JsonNode.class);


            String resultCode = rootNode.get("status").asText();
            // 成功
            if ("0".equals(resultCode)) {
                JsonNode resultNode = rootNode.get("result");
                ponse.setStatus(900000);
                ponse.setMessage("获取成功");
                ponse.setParams(resultNode);
            } else if ("201".equals(resultCode)) {
                //银行卡号为空
                ponse.setStatus(900105);
                ponse.setMessage("银行卡号为空");
            } else if ("202".equals(resultCode)) {
                //交易失败，请稍后重试
                ponse.setStatus(900106);
                ponse.setMessage("银行卡号不正确");
            } else {
                ponse.setStatus(900107);
                ponse.setMessage("没有信息");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ponse;
    }

    public static ApiResponse checkBank(Bank bank, Member member) {
        ApiResponse ponse = new ApiResponse();
        String host = "http://jisubank4.market.alicloudapi.com";
        String path = "/bankcardverify4/verify";
        String method = "GET";
        String appcode = "2367dde9fcb5418c86b13a0bd8a3e7f6";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("bankcard", bank.getCardNo());
        querys.put("idcard", member.getIdentityNo());
        querys.put("mobile", bank.getPhone());
        querys.put("realname", member.getName());

        try {
            HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
            String resultBody = EntityUtils.toString(response.getEntity());
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode rootNode;
            rootNode = objectMapper.readValue(resultBody, JsonNode.class);
            String resultCode = rootNode.get("status").asText();
            // 成功
            if ("0".equals(resultCode)) {
                JsonNode resultNode = rootNode.get("result");
                String verifystatus = resultNode.get("verifystatus").asText();
                if ("0".equals(verifystatus)) {
                    ponse.setStatus(900000);
                    ponse.setMessage("银行卡添加成功");
                } else {
                    ponse.setStatus(900114);
                    ponse.setMessage(resultNode.get("verifymsg").asText());
                }
            } else if ("201".equals(resultCode)) {
                //银行卡号为空
                ponse.setStatus(900108);
                ponse.setMessage("银行卡号为空");
            } else if ("202".equals(resultCode)) {
                //真实姓名为空
                ponse.setStatus(900109);
                ponse.setMessage("真实姓名为空");
            } else if ("203".equals(resultCode)) {
                //银行卡号不正确
                ponse.setStatus(900110);
                ponse.setMessage("银行卡号不正确");
            } else if ("204".equals(resultCode)) {
                //真实姓名包含特殊字符
                ponse.setStatus(900111);
                ponse.setMessage("真实姓名包含特殊字符");
            } else if ("205".equals(resultCode)) {
                //真实姓名包含特殊字符
                ponse.setStatus(900112);
                ponse.setMessage("身份证不正确");
            } else {
                ponse.setStatus(900113);
                ponse.setMessage("没有信息");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ponse;
    }
}
